//
//  ViewController.m
//  线程安全问题展示
//
//  Created by sugar on 15/10/21.
//  Copyright (c) 2015年 sugar. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIAlertViewDelegate>
{
    UITextField *tf;
    UIButton *btn1,*btn2,*btn3,*btn4;
    UIAlertView *alert;
}
@end
NSLock *lock;// 线程锁🔒
@implementation ViewController
-(id)init{
    if (self=[super init]) {
        lock=[[NSLock alloc]init];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    tf=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 100, 30)];
    tf.text=@"100";
    tf.backgroundColor=[UIColor lightGrayColor];
    tf.borderStyle=UITextBorderStyleRoundedRect;
    
    // btn1
    btn1=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn1.frame=CGRectMake(100, 200, 100, 30);
    [btn1 setTitle:@"买票窗口A" forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(clickBtn1) forControlEvents:UIControlEventTouchUpInside];
    
    // btn2
    btn2=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn2.frame=CGRectMake(100, 300, 100, 30);
    [btn2 setTitle:@"买票窗口B" forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(clickBtn2) forControlEvents:UIControlEventTouchUpInside];
    
    // btn3
    btn3=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn3.frame=CGRectMake(100, 400, 100, 30);
    [btn3 setTitle:@"买票窗口C" forState:UIControlStateNormal];
    [btn3 addTarget:self action:@selector(clickBtn3) forControlEvents:UIControlEventTouchUpInside];
    
    // 设置票数
    // btn1
    btn4=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn4.frame=CGRectMake(200, 60, 100, 30);
    [btn4 setTitle:@"放 票" forState:UIControlStateNormal];
    [btn4 addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];

    // 加载视图
    [self.view addSubview:tf];
    [self.view addSubview:btn1];
    [self.view addSubview:btn2];
    [self.view addSubview:btn3];
    [self.view addSubview:btn4];
    // 设置原始票数
    self.leftTicketsCount=100;
    // 代理
    alert.delegate=self;
    // lock 初始化
    lock=[[NSLock alloc]init];
    [NSTimer scheduledTimerWithTimeInterval:0.00001 target:self selector:@selector(changeValue) userInfo:nil repeats:YES];

}
-(void)clickBtn1{
    // 创建新线程
    NSThread *threadA=[[NSThread alloc]initWithTarget:self selector:@selector(test) object:nil];
    threadA.name=@"售票员贾梦丹";;
    [threadA start];
}
-(void)clickBtn2{
    // 创建新线程
//    NSThread *threadB=[[NSThread alloc]initWithTarget:self selector:@selector(test) object:nil];
    threadB.name=@"售票员李鑫";;
    [threadB start];
}
-(void)clickBtn3{
    // 创建新线程
    NSThread *threadC=[[NSThread alloc]initWithTarget:self selector:@selector(test) object:nil];
    threadC.name=@"售票员张静怡";;
    [threadC start];
}
//  A：100  B：100  A：98  B：99
// A：100  A：99 96  B
-(void)test{
//    [lock lock];
//    if (self.leftTicketsCount>0) {
//        self.leftTicketsCount--;
//        NSLog(@"--%@--%d",[NSThread currentThread],self.leftTicketsCount);
//    }
//    [lock unlock];
    //    🔒 加锁
    [lock lock];
for (self.leftTicketsCount=5000; self.leftTicketsCount>0; ) {
    
    if (self.leftTicketsCount >0) {
        self.leftTicketsCount--;
        NSLog(@"-%@-剩余票数:%d",[NSThread currentThread],self.leftTicketsCount);
    }
    
    }
    [lock unlock];
    //    🔒 解锁
    
//    for (i=self.leftTicketsCount; self.leftTicketsCount>0; self.leftTicketsCount--) {
//        // 获取当前线程
//        NSLog(@"-%@-剩余票数:%d",[NSThread currentThread],self.leftTicketsCount-1);
//    }

    
    
}

-(void)click{
//    // 设置票数
//    alert=[[UIAlertView alloc]initWithTitle:@"设置票数" message:@"请设置此次发放票数" delegate:self cancelButtonTitle:@"CANCEL" otherButtonTitles:@"OK", nil];
//    alert.alertViewStyle=UIAlertViewStylePlainTextInput;
//    [alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==1) {
        tf.text=[alert textFieldAtIndex:0].text;
        NSLog(@"%@",tf.text);
        self.leftTicketsCount=tf.text.intValue;
    }
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [tf resignFirstResponder];
}
-(void)changeValue{
    tf.text=[NSString stringWithFormat:@"%d",self.leftTicketsCount];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
